package idv.arthur.svn;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNProperties;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNCopyClient;
import org.tmatesoft.svn.core.wc.SVNCopySource;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class CopyTrunk {

	public static void main(String[] args) {
		
		String svnOriPath = "trunk";
		String svnFinalPath = svnOriPath;
		ISVNEditor editor = null;
		SVNRepository apRepo = null;
		ISVNAuthenticationManager authManager = null;
		String url = "http://10.144.97.99/Arthur/";
		SVNCommitInfo info = null;
        try {
        	apRepo = SVNRepositoryFactory.create(SVNURL.parseURIEncoded( url ));	//連線到SVN AP OL 的repo
            authManager = SVNWCUtil.createDefaultAuthenticationManager("Arthur", "arthur7382");
            apRepo.setAuthenticationManager(authManager);
            editor = apRepo.getCommitEditor(null, new CommitMediator() );
            editor.openRoot(-1);
        	if ( "trunk".equalsIgnoreCase(svnOriPath)) {
            	//以今天為日期，建立資料夾
            	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_");
        		String today = formatter.format( new Date() );
        		editor.openDir("branch", -1);
        		int i=1;
        		while ( true ) {
        			
        			String folder = String.format(today+"%02d", i++);	//預計在 ap repo中創建的夾名
        			
        	        try {	//若能夠打開該路徑的資料夾，便是該夾存在，否則便進入catch來創建該資料夾
        	        	editor.addDir(folder, null, -1);
                		editor.closeDir();
                		
                		svnFinalPath = "branch/"+folder;
                		break;
                	} catch ( Exception e) {
                		editor.closeDir();
                		//能走到這行，表示沒有進入Exception
                		//因此開始複制 原本 trunk的內容到新建立的夾
                		
                		
                	}
        		}
        		SVNCopyClient copyClient = new SVNCopyClient(authManager, new DefaultSVNOptions() );
            	
            	SVNCopySource cs = new SVNCopySource(SVNRevision.HEAD,SVNRevision.HEAD,SVNURL.parseURIEncoded( url+"trunk" ));
            	
            	SVNCopySource[] sources = new SVNCopySource[1];
            	sources[0] = cs;
            	System.out.println("要執行了");
            	System.out.println("來源"+cs.getURL().getURIEncodedPath());
            	info = copyClient.doCopy(sources, SVNURL.parseURIEncoded( url+svnFinalPath ), false, false, false, "程式創建", new SVNProperties());
            	System.out.println("目的"+url+svnFinalPath);
    		}
        	//editor.closeEdit();//此時已有了資料夾，開始進行複制
        	System.out.println("現在的版次"+ info.getNewRevision() );
        	
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        	if ( apRepo!= null ) { apRepo.closeSession(); }
        }
        
		

	}

}
